package cn.itcast.controller;

import cn.itcast.domain.User;
import cn.itcast.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    IUserService userService;

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/turnToLogin")
    public String turnToLogin(){
        return "login";
    }

    @RequestMapping("/turnToRegister")
    public String turnToRegister(){
        return "register";
    }

    @RequestMapping("/mine")
    public String mine(){
        return "mine";
    }

    //跳转至搜索页面
    @RequestMapping("/turnToSearch")
    public String turnToSearch(){
        return "search";
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public void register(User registerUser, HttpServletResponse response){
        List<User> userList = userService.findAllUser();
        boolean flag = true;
        for(User user:userList){
            //查询用户名是否重复
            if(user.getUsername().equals(registerUser.getUsername())){
                flag = false;
            }
        }
        if(flag){
            userService.insertUser(registerUser);
        }
        try {
            response.getWriter().print(flag);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        if(flag){
//            //若用户名不重复,插入新用户，并携带成功信息,跳转至登录
//            userService.insertUser(newUser);
//            model.addAttribute("msg","注册成功，请登录");
//            return "login";
//        } else{
//            //若用户名重复,携带失败信息，跳转至注册
//            model.addAttribute("msg","用户名重复，请重新注册");
//            return "register";
//        }
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public void login(User loginUser, HttpServletResponse response){
        List<User> userList = userService.findAllUser();
        boolean flag = false;
        for(User user : userList){
            if(user.getUsername().equals(loginUser.getUsername())){
                //若用户名存在，则判断密码是否匹配
                if(user.getPassword().equals(loginUser.getPassword())) {
                    //密码存在，设flag为true
                    flag = true;
                    //生成一个token对象,保存在redis中
                    redisTemplate.opsForHash().put("token","user",user);
                }
            }
        }
        try {
            response.getWriter().print(flag);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        if(flag) {
//            //登录成功
//            model.addAttribute("msg","登录成功!");
//            return "redirect: donateGoods";
//        } else{
//            //登录失败
//            model.addAttribute("msg","用户名或密码错误,登录失败");
//            return "login";
//        }
    }

    //以JSON字符串形式返回token
    @RequestMapping(value = "/getToken",method = RequestMethod.GET)
    @ResponseBody
    public User getToken(){
        User user = (User) redisTemplate.opsForHash().get("token", "user");
        return user;
    }

    /**
     * 用户注销
     */
    @RequestMapping("/logout")
    public String logout(){
        redisTemplate.opsForHash().delete("token","user");
        return "/login";
    }

}
